﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _4DJackPot
{
    public partial class FileDialog
    {
        private string filter = "";
        private string title = "";
        private string fileDirectory = "";
        private string fileName = "";

        public FileDialog()
	    {
		    
	    }

        // Open File Dialog
	    public void OpenDialog() 
	    {
		     // Setup for OpenFileDialog Object
		    OpenFileDialog openFileDialog = new OpenFileDialog();
            // Setup Init Directory
            openFileDialog.InitialDirectory = System.Windows.Forms.Application.StartupPath;
		    // Setup Filter
            openFileDialog.Filter = this.filter;
            // Setup Title
            openFileDialog.Title = this.title;

            // Success to File Open Dialog
		    System.Windows.Forms.Form dialogWindow = new System.Windows.Forms.Form();
		    if(openFileDialog.ShowDialog(dialogWindow) == DialogResult.OK) 
		    {
			    string fullFileNameString = openFileDialog.FileName;
			    string[] fullFileNameToken = fullFileNameString.Split('\\');
			    // Open File Name
			    string fileNameString = fullFileNameToken[fullFileNameToken.Length - 1];
			    // Open File Directory
			    string fileDirectoryString = "";
			    for(int i=0; i<fullFileNameToken.Length - 1; i++) 
			    {
				    if(i == 0) 
				    {
					    fileDirectoryString += fullFileNameToken[i];
				    }
				    else fileDirectoryString += "\\" + fullFileNameToken[i];
			    }
			    this.fileName = fileNameString;
			    this.fileDirectory = fileDirectoryString;
		    }
		    else 
		    {
			    this.fileName = "";
			    this.fileDirectory = "";
		    }
	    }

        // Open File Dialog
        public void OpenDialog(string initDirectoryString)
        {
            // Setup for OpenFileDialog Object
            OpenFileDialog openFileDialog = new OpenFileDialog();
            // Setup Init Directory
            openFileDialog.InitialDirectory = initDirectoryString;
            // Setup Filter
            openFileDialog.Filter = this.filter;
            // Setup Title
            openFileDialog.Title = this.title;

            // Success to File Open Dialog
            System.Windows.Forms.Form dialogWindow = new System.Windows.Forms.Form();
            if (openFileDialog.ShowDialog(dialogWindow) == DialogResult.OK)
            {
                string fullFileNameString = openFileDialog.FileName;
                string[] fullFileNameToken = fullFileNameString.Split('\\');
                // Open File Name
                string fileNameString = fullFileNameToken[fullFileNameToken.Length - 1];
                // Open File Directory
                string fileDirectoryString = "";
                for (int i = 0; i < fullFileNameToken.Length - 1; i++)
                {
                    if (i == 0)
                    {
                        fileDirectoryString += fullFileNameToken[i];
                    }
                    else fileDirectoryString += "\\" + fullFileNameToken[i];
                }
                this.fileName = fileNameString;
                this.fileDirectory = fileDirectoryString;
            }
            else
            {
                this.fileName = "";
                this.fileDirectory = "";
            }
        }

	    // Save File Dialog
	    public void SaveDialog() 
	    {
		    SaveFileDialog saveFileDialog = new SaveFileDialog();
            // Setup Init Directory
            saveFileDialog.InitialDirectory = System.Windows.Forms.Application.StartupPath;
		    // Setup Filter
            saveFileDialog.Filter = this.filter;
		    // Setup Title
            saveFileDialog.Title = this.title;

		    // Success to File Save Dialog
		    System.Windows.Forms.Form dialogWindow = new System.Windows.Forms.Form();
		    if(saveFileDialog.ShowDialog(dialogWindow) == DialogResult.OK) 
		    {
			    string fullFileNameString = saveFileDialog.FileName;
			    string[] fullFileNameToken = fullFileNameString.Split('\\');
			    // Save File Name
			    string fileNameString = fullFileNameToken[fullFileNameToken.Length - 1];
			    // Save File Directory
			    string fileDirectoryString = "";
			    for(int i=0; i<fullFileNameToken.Length - 1; i++) 
			    {
				    if(i == 0) 
				    {
					    fileDirectoryString += fullFileNameToken[i];
				    }
				    else fileDirectoryString += "\\" + fullFileNameToken[i];
			    }
			    this.fileName = fileNameString;
			    this.fileDirectory = fileDirectoryString;
		    }
		    else 
		    {
			    this.fileName = "";
			    this.fileDirectory = "";
		    }
	    }
	    // Save File Dialog
	    public void SaveDialog(string initDirectoryString, string initFileString) 
	    {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            // Setup Init Directory
            saveFileDialog.InitialDirectory = initDirectoryString;
            saveFileDialog.FileName = initFileString;
            // Setup Filter
            saveFileDialog.Filter = this.filter;
            // Setup Title
            saveFileDialog.Title = this.title;
            
            // Success to File Save Dialog
            System.Windows.Forms.Form dialogWindow = new System.Windows.Forms.Form();
            if (saveFileDialog.ShowDialog(dialogWindow) == DialogResult.OK)
            {
                string fullFileNameString = saveFileDialog.FileName;
                string[] fullFileNameToken = fullFileNameString.Split('\\');
                // Save File Name
                string fileNameString = fullFileNameToken[fullFileNameToken.Length - 1];
                // Save File Directory
                string fileDirectoryString = "";
                for (int i = 0; i < fullFileNameToken.Length - 1; i++)
                {
                    if (i == 0)
                    {
                        fileDirectoryString += fullFileNameToken[i];
                    }
                    else fileDirectoryString += "\\" + fullFileNameToken[i];
                }
                this.fileName = fileNameString;
                this.fileDirectory = fileDirectoryString;
            }
            else
            {
                this.fileName = "";
                this.fileDirectory = "";
            }
	    }
	    // Get File Name
	    public string GetFileName() 
	    {
		    return this.fileName ?? "";
	    }
	    // Get File Directory
	    public string GetFileDirectory() 
	    {
            return this.fileDirectory ?? "";
	    }
	    // Set Filter
	    public void SetFilter(string filterString) 
	    {
            this.filter = filterString ?? "";
	    }
	    // Set Title
	    public void SetTitle(string titleString) 
	    {
            this.title = titleString ?? "";
	    }
    }
}
